#include "usart2.h"
uint8_t Usart2_RXBuff[Usart2_RXBuffSize];



/******** 激光测距 **********/



void uart2_init(uint32_t baudrate)
{
    gpio_init_type gpio_init_struct;

    /* enable the uart and gpio clock */
    crm_periph_clock_enable(CRM_USART2_PERIPH_CLOCK, TRUE);
    crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);

    gpio_default_para_init(&gpio_init_struct);

    /* configure the uart tx pin */
    gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
    gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
    gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
    gpio_init_struct.gpio_pins = GPIO_PINS_2 | GPIO_PINS_3;
    gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
    gpio_init(GPIOA, &gpio_init_struct);
    gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE2, GPIO_MUX_7);
    gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE3, GPIO_MUX_7);

    nvic_irq_enable(USART2_IRQn, usart2_preempt_priority, usart2_sub_priority);

    /* configure uart param */
    usart_init(USART2, baudrate, USART_DATA_8BITS, USART_STOP_1_BIT);

    usart_transmitter_enable(USART2, TRUE);
    usart_receiver_enable(USART2, TRUE);

    usart_interrupt_enable(USART2, USART_RDBF_INT, TRUE);
    usart_interrupt_enable(USART2, USART_IDLE_INT, TRUE);

    usart_enable(USART2, TRUE);
}

/**
 * @brief  this function handles usart2 handler.  LRF
 * @param  none
 * @retval none
 */
void USART2_IRQHandler(void)
{
	static uint8_t Usart2_index = 0;
	uint8_t clear;
	BaseType_t xHigherPriorityTaskWoken = pdFALSE;
	
	if(usart_flag_get(USART2, USART_RDBF_FLAG) != RESET)			// USART2接收中断响应
	{
		//clear = USART2->dt;
		Usart2_RXBuff[Usart2_index] = USART2->dt;
		Usart2_index ++;
//		usart_flag_clear(USART2,USART_RDBF_FLAG);
	}
	if(usart_flag_get(USART2, USART_IDLEF_FLAG) != RESET)			// USART2接收中断响应
	{
		Usart2_RXBuff[Usart2_index] = '\r';
		xQueueSendFromISR(Distance_QueueHandle,(void*)Usart2_RXBuff,0);				//发送队列
		
//		clear = USART2->sts;										// USART3清除空闲中断标志位
//		clear = USART2->dt;											// USART3清除空闲中断标志位
		Usart2_index = 0;
		//usart_flag_clear(USART2,USART_IDLEF_FLAG);
		
	}
	
	if(Usart2_index > 255) Usart2_index = 0;

usart_flag_clear(USART2,USART_IDLEF_FLAG);
usart_flag_clear(USART2,USART_RDBF_FLAG);
}

void Uart2Send(uint8_t *buff,uint8_t size)
{
	uint8_t *Enter_click = buff;
	
	for (uint8_t uart2_tx_cnt = 0; uart2_tx_cnt < size; uart2_tx_cnt++)
	{
		while (usart_flag_get(USART2, USART_TDBE_FLAG) == RESET)
			;
		usart_data_transmit(USART2, Enter_click[uart2_tx_cnt]);
		while (usart_flag_get(USART2, USART_TDC_FLAG) == RESET)
			;
	}
}
































